<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01.引出生命周期钩子</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
</head>
<body>
    <div id="app"></div>
    <script type="text/babel">
        class Demo extends React.Component{
            state = {
                opacity: 1
            }
            // 生命周期钩子函数 在组件挂在完成后触发
            componentDidMount(){
                console.log('componentDidMount() invoked!');
                let { opacity } = this.state
                this.timer = setInterval(() => {
                    opacity = opacity < 0 ? 1 : opacity - 0.1
                    this.setState({
                        opacity
                    })
                }, 200)
            }

            //生命周期钩子函数，在组件卸载之前固定触发
            componentWillUnmount(){
                console.log('componentWillUnmount() invoked!');
                clearInterval(this.timer)
            }
            
            render(){
                console.log('render() invoked!')
                const {opacity} = this.state
                return (
                    <div>
                        <h1 style={{opacity}}>孩子学习老不好，多半是废了。</h1>
                        <button onClick={this.unmount}>躺平吧</button>
                    </div> 
                )
            }

            unmount = () => {
                // ReactDOM.unmountComponentAtNode(DOM Node) 卸载节点内的组件
                ReactDOM.unmountComponentAtNode(document.querySelector('#app'))
            }
        }

        ReactDOM.render(<Demo/>, document.querySelector('#app'))
    </script>
</body>
</html>